import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:file_picker_desktop/file_picker_desktop.dart';
import 'package:tuple/tuple.dart';
import 'package:get/get.dart';
import 'package:rename_exif/main_controller.dart';
import 'main_controller.dart';
import 'tools.dart';

class Options extends StatelessWidget {
  const Options({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var c = Get.find<MainController>();
    return Container(
      alignment: Alignment.centerLeft,
      padding: const EdgeInsets.only(left: 10, right: 10),
      color: Colors.amber[800],
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Obx(() => MyDropdownButton(
            label: "文件名样式:",
            items: FileNameStyleMap,
            selectedSort: c.fileNameStyleSelected.value,
            onChanged: (s) => c.fileNameStyleSelected(s),
          )), VD,
          Obx(() => MyDropdownButton(
            label: "无Exif时处理:",
            items: NotExifMap,
            selectedSort: c.notExifSelected.value,
            onChanged: (s) => c.notExifSelected(s),
          )), VD,
          Obx(() => MyDropdownButton(
            label: "文件保存:",
            items: SaveModeMap,
            selectedSort: c.saveModeSelected.value,
            onChanged: (s) async {
              if (s == "1") {
                c.saveMode2Dir = null;
                c.saveModeSelected(s);
              } else if (s == "2") {
                showMyCupertinoDialog(context);
                try {
                  var p = await getDirectoryPath(dialogTitle: "选择新目录");
                  if (p == null) { return; }
                  c.saveMode2Dir = p;
                  c.saveModeSelected(s);
                } finally {
                  Navigator.of(context).pop();
                }
              }
            }
          )), VD,
          Container(width: 200, padding: const EdgeInsets.only(bottom: 7.5),
            child: TextField(
              textAlign: TextAlign.center,
              maxLines: 1,
              decoration: InputDecoration(
              border: const UnderlineInputBorder(),
              hintText: r"添加文件名前缀",
              hintStyle: Theme.of(context).textTheme.bodyText2?.copyWith(fontStyle: FontStyle.italic),
              ),
              onChanged: c.setPrefix,
          )),
      ]),
    );
  }
}

// ignore: must_be_immutable
class MyDropdownButton extends StatelessWidget {
  MyDropdownButton({Key? key,
    required this.label,
    required this.items,
    required this.selectedSort,
    required this.onChanged,
  }) : super(key: key);

  String label;
  Function(String) onChanged;
  List<Tuple2<String, String>> items;
  String selectedSort;

  @override
  Widget build(BuildContext context) {
    var sortItems = items.map((e){
      return DropdownMenuItem(
        value: e.item1,
        child : Padding(
          padding: const EdgeInsets.only(left: 5, right: 5),
          child: Text(e.item2, style: Theme.of(context).textTheme.bodyText2?.copyWith(fontStyle: FontStyle.italic)),
        ));
    }).toList();

    return Row(children: [
      Text(label, style: Theme.of(context).textTheme.subtitle1?.copyWith(fontWeight: FontWeight.w500)),
      const SizedBox(width: 5),
      DropdownButton(
        // underline: Container(),
        value: selectedSort,
        items: sortItems,
        onChanged: (String? s) => s == null ? null : onChanged(s),
      )
    ]);
  }
}